//
//  ShakeViewController.m
//  Choice
//
//  Created by A-01 on 2020/6/16.
//  Copyright © 2020 Mac. All rights reserved.
//

#import "ShakeViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
#import <CoreAudio/CoreAudioTypes.h>

@interface ShakeViewController ()

@property (nonatomic, weak)UIImageView * iconImageView;
@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, weak)UIToolbar *toolbar;
@property (nonatomic, weak)UIView * whiteView;

@end

@implementation ShakeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"Shake";
    self.view.backgroundColor = ZRWhiteColor;
    [self setupUI];
}

- (void)setupUI{
      
    UIImageView * iconImageView = [UIImageView new];
    
    iconImageView.image = [UIImage imageNamed:@"shake"];
    
    self.iconImageView = iconImageView;
    
    [self.view addSubview:self.iconImageView];
    
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.text = @"Shake your cell phone,please.";
    titleLabel.textColor = [UIColor colorWithHexString:@"BABABA"];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:16];
    
    self.titleLabel = titleLabel;
    
    [self.view addSubview:self.titleLabel];
}

#pragma mark 摇动开始
- (void)motionBegan:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event
{

    if (motion ==UIEventSubtypeMotionShake )
    {
        //设置震动
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    }

}

#pragma mark -
#pragma mark 摇动结束
- (void)motionEnded:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event
{

    [self setupToolBarView];
}

- (void)viewDidLayoutSubviews{
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.view).offset(-50);
        make.width.height.mas_equalTo(130);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.iconImageView.mas_bottom).offset(15);
    }];
}

- (void)setupToolBarView{
    
    UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:[UIScreen mainScreen].bounds];
    //样式
    toolbar.barStyle = UIBarStyleBlack;//半透明
    //透明度
    toolbar.backgroundColor = ZRBlackColor;
    toolbar.alpha = 0.5f;
    toolbar.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickCloseButton)];
    [toolbar addGestureRecognizer:tapGR];
    
    self.toolbar = toolbar;
    [self.navigationController.view addSubview:self.toolbar];
    
    UIView * whiteView = [[UIView alloc] init];
    
    whiteView.backgroundColor = ZRWhiteColor;
    whiteView.userInteractionEnabled = YES;
    whiteView.layer.cornerRadius = 5;
    
    self.whiteView = whiteView;
    
    [self.navigationController.view addSubview:self.whiteView];

    
    UIButton * closeButton = [UIButton new];
    
    [closeButton setImage:[UIImage imageNamed:@"cancle"] forState:UIControlStateNormal];
    [closeButton addTarget:self action:@selector(clickCloseButton) forControlEvents:UIControlEventTouchUpInside];
    
    [self.whiteView addSubview:closeButton];
    
    UIImageView * toyImageView = [UIImageView new];
    
    toyImageView.image = [UIImage imageNamed:@"small_toy"];
    
    [self.whiteView addSubview:toyImageView];
    
    
    UILabel * titleLab = [UILabel new];
    
    titleLab.text = @"Best Choice";
    titleLab.textColor = ZRBlackColor;
    titleLab.font = [UIFont boldSystemFontOfSize:16];
    titleLab.textAlignment = NSTextAlignmentCenter;
    
    [whiteView addSubview:titleLab];
    
    
    UILabel * reslutLab = [UILabel new];
    
    NSArray * arr = [[NSUserDefaults standardUserDefaults] objectForKey:self.type];
    int x = arc4random() % arr.count;
    reslutLab.text = arr[x];
    reslutLab.textColor = [UIColor colorWithHexString:@"F26760"];
    reslutLab.font = [UIFont boldSystemFontOfSize:24];
    reslutLab.textAlignment = NSTextAlignmentCenter;
    
    [whiteView addSubview:reslutLab];
    
    
    [self.whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.view);
        make.width.mas_equalTo(176);
        make.height.mas_equalTo(194);
    }];
    
    [closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.whiteView.mas_bottom).offset(20);
        make.width.height.mas_equalTo(48);
    }];
    
    [toyImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.whiteView.mas_top);
        make.width.mas_equalTo(144);
        make.height.mas_equalTo(100);
    }];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.whiteView);
        make.width.mas_equalTo(150);
        make.top.mas_equalTo(toyImageView.mas_bottom).offset(5);
    }];

    [reslutLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.whiteView);
        make.width.mas_equalTo(150);
        make.top.mas_equalTo(titleLab.mas_bottom).offset(30);
    }];
}


- (void)clickCloseButton{
    
    [self.toolbar removeFromSuperview];
    [self.whiteView removeFromSuperview];
}


@end
